﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utils;
//基本工资项列表，也可以修改、删除， 但是该界面上不能增加
//可以通过EmpSetsList.aspx 增加修改个人的基本工资信息
namespace UI.ModSalary
{
    public partial class EmpItemList : System.Web.UI.Page
    {
        EmpItemBiz empItemBiz = new EmpItemBiz();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNoInfo.Attributes.Add("onclick", "javascript:ShowLink('EmpItemNo.aspx',800,600);");
                btnNoAccount.Attributes.Add("onclick", "javascript:ShowLink('EmpItemBankNo.aspx',800,600);");
                btnIn.Attributes.Add("onclick", "javascript:ShowLink('EmpItemImport.aspx',700,480);");
                new DepartmentBiz().InitDropDownList(ddlDeptID, "0");
                DefDicBiz defDicBiz = new DefDicBiz();
                defDicBiz.InitDropDownList("Wage_Sets", ddlSetsID, "1");
                //if (Session["user"] != null)
                //{
                //    //User user = Session["user"] as User;
                //    //iAccountID = user.AccountID;

                //}
                //else//请登陆
                //{
                //    Response.Write("你没有登录！");
                //    Response.End();
                //}


            }
        }

        protected void btnEmSearch_Click(object sender, EventArgs e)
        {
            string empCode = txtSeWord.Text.Trim();
            string setsID = ddlSetsID.SelectedValue.ToString();
            string deptName = ddlDeptID.SelectedItem.Text;
            if (string.IsNullOrWhiteSpace(empCode))
            {
                if (setsID == "" || setsID == "0")
                {
                    Utils.JSUtility.Alert(this, "请选择帐套");
                    return;
                }
            }

            BindGv(empCode, deptName, setsID);
        }
        protected void BindGv(string empCode, string deptName, string setsID)
        {
            DataTable dt = empItemBiz.GetLists(empCode,deptName, setsID);
            int r = dt.Rows.Count;

            if (r > 0)
            {   gvList.DataSource = dt;
                gvList.DataBind();
               // GridViewBind(gvList, dt, empCode, setsID);
                lbAmount.Text = "总人数：" + r.ToString();
            }
        }

        public static void GridViewBind(GridView gdv, DataTable dtblDataSource, string strDataKey, string setsID)
        {
          // gdv.Columns.Clear();

            gdv.AutoGenerateColumns = false;
            gdv.DataSource = dtblDataSource;
           // gdv.DataKeyNames = new string[] { strDataKey };
            //取出目标列
            Hashtable ht = new SetsItemBiz().GetFieldsHashTable(setsID);
            for (int i = 0; i < dtblDataSource.Columns.Count; i++)   //绑定普通数据列
            {

                string columName = dtblDataSource.Columns[i].ColumnName;
                if (ht.ContainsKey(columName))
                {
                    
                    BoundField bfColumn = new BoundField();
                    bfColumn.DataField = columName;
                    bfColumn.HeaderText = ht[columName].ToString();                   
                    gdv.Columns.Add(bfColumn);
                }
            }

            //CommandField cfModify = new CommandField();  //绑定命令列
            //cfModify.ButtonType = ButtonType.Button;
            //cfModify.SelectText = "修改";
            //cfModify.ShowSelectButton = true;
            //gdv.Columns.Add(cfModify);
            gdv.DataBind();
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string empCode = e.Row.Cells[1].Text;
                string setsID = ddlSetsID.SelectedValue.ToString();
                e.Row.Cells[1].Text = " <a href=\"javascript:ShowLink('EmpItemAdd.aspx?EmpCode=" + empCode + "&SetsID=" + setsID + "',880,610)\">" + empCode + "</a>";
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string empCode = txtSeWord.Text.Trim();
            string setsID = ddlSetsID.SelectedValue.ToString();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                Utils.JSUtility.Alert(this, "请选输入工号");
                return;
            }
            if (setsID == "" || setsID == "0")
            {
                Utils.JSUtility.Alert(this, "请选择帐套");
                return;
            }

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "js", @"<script language='javascript'>ShowLink('EmpItemAdd.aspx?EmpCode=" + empCode + "&SetsID=" + setsID + "',780,610);</script>");

        }
        //导出
        protected void btnOut_Click(object sender, EventArgs e)
        {
            string empCode = txtSeWord.Text.Trim();
            string setsID = ddlSetsID.SelectedValue.ToString();
            string deptName = ddlDeptID.SelectedItem.Text;
            if (string.IsNullOrWhiteSpace(empCode))
            {
                if (setsID == "" || setsID == "0")
                {
                    Utils.JSUtility.Alert(this, "请选择帐套");
                    return;
                }
            }


            DataTable resultDt = empItemBiz.GetOutList(empCode, deptName, setsID);
            ExcelUtility.ExportDsToXls(this, resultDt, "EmpItem.xls");
        }

       
        
    }
}